/*Test Cases Created by Steven Chow
 * This Test Cases tests for deleting
 * all tuples in each database 
 * All Tests are independant from each other.
 */

package com.JUnitTestsDB;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.database.AdministratorDB;
import com.database.CustomerDB;
import com.database.FlightDB;
import com.database.ReservationDB;
import com.database.SeatDB;

public class DeleteAllDBTest {

	private Connection connection;
	
	//Setup Connection for Test
	public DeleteAllDBTest()
	{
		String connectionString =
			    "jdbc:mysql://localhost/planesimple?"  //CHANGE THIS PART!!!
              + "user=bluebear&password=bluebeariscute89";
		try
        {
            // Ensure the SQL Server driver class is available.
            Class.forName("com.mysql.jdbc.Driver");

            // Establish the connection.
            connection = DriverManager.getConnection(connectionString);
        }
        // Exception handling
        catch (ClassNotFoundException cnfe)  
        {

            System.out.println("ClassNotFoundException " +
                               cnfe.getMessage());
        }
        catch (Exception e)
        {
            System.out.println("Exception " + e.getMessage());
            e.printStackTrace();
        }
	}
	
	//Gets the number of tuples in the Database
	private int getTupleCount(String string) {
		int count = 0;
	    String sqlString = "SELECT * FROM " + string;
	    Statement stmt;
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sqlString);
			while (rs.next())
			{
				count++;
			}	
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!");
		}
		return count;
	}
	
	//Test 1 Delete all tuples from Admin , Customer, Reservation and Flight
	@Test
	public void DeleteAllTest() {
		AdministratorDB db1 = new AdministratorDB();
		CustomerDB db2 = new CustomerDB();
		ReservationDB db3 = new ReservationDB();
		FlightDB db4 = new FlightDB();
		SeatDB db5 = new SeatDB();
		db1.Setup(connection);
		db2.Setup(connection);
		db3.Setup(connection);
		db4.Setup(connection);
		db5.Setup(connection);
		try {
			db5.DeleteAll();
			db3.DeleteAll();
			db1.DeleteAll();
			db2.DeleteAll();
			db4.DeleteAll();
			//Check if empty
			assertEquals(0, getTupleCount("Reservation"));
			assertEquals(0, getTupleCount("Customer"));
			assertEquals(0, getTupleCount("Administrator"));
			assertEquals(0, getTupleCount("Flight"));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			e.printStackTrace();
			assertEquals(0, getTupleCount("Reservation"));
			assertEquals(0, getTupleCount("Customer"));
			assertEquals(0, getTupleCount("Administrator"));
			assertEquals(0, getTupleCount("Flight"));
		}
	}
}
